﻿using Dealin.Code;
using Dealin.Data;
using Dealin.Data.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dealin.Application.ReportManage
{
    public class ReportsApp
    {

        //public DealinDbContext dbcontext = new DealinDbContext();

        public DapperDbHelper db = new DapperDbHelper();
        public string GetDefaultJson(string orgId)
        {
            string proc = string.Format("Proc_Home '{0}'", orgId);
            // var Results = dbcontext.MultipleResults(proc)
            //.AddResult<double>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.Execute();

            // return Results.ToJson();
            List<dynamic> ret = new List<dynamic>();

            var list = db.QueryMultiple(proc, null, 5, System.Data.CommandType.Text);

            //总电量 当日电量 kWh
            ret.Add(list[0][0]);

            //当日各站点 电量分布 kW 
            LineChart kW = new LineChart();
            kW.xAxis = new List<dynamic>() { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", };
            kW.legend = new List<dynamic>();
            kW.series = new List<LineSerie>();
            if (list[1] != null && list[1].Count > 0)
            {
                List<dynamic> il = list[1];
                var orgs = il.Select(o => o.OrgName).Distinct();
                kW.legend = orgs.ToList();

                foreach (string org in orgs)
                {
                    var data = from p in il
                               where p.OrgName.ToString() == org
                               select p;
                    LineSerie sr = new LineSerie()
                    {
                        type = "line",
                        name = org,
                        smooth = true,
                        data = new List<double>()
                    };

                    foreach (string time in kW.xAxis)
                    {
                        var val = data.FirstOrDefault(o => o.H.ToString() == time);

                        if (val == null) sr.data.Add(0);
                        else sr.data.Add(val.kW);
                    }
                    kW.series.Add(sr);
                }


            }
            ret.Add(kW);

            //环比 电量分布
            PieChart pie = new PieChart();
            pie.legend = new List<dynamic>();
            pie.seriesData = new List<PieData>();
            if (list[2] != null && list[2].Count > 0)
            {
                List<dynamic> il = list[2];
                var energys = il.Select(o => o.Energy).Distinct();
                pie.legend = energys.ToList();
                foreach (string en in energys)
                {
                    var val = il.FirstOrDefault(o => o.Energy.ToString() == en);
                    if (val == null) continue;
                    pie.seriesData.Add(new PieData()
                    {
                        name = val.Energy,
                        value = val.kWh
                    });
                }
            }
            ret.Add(pie);


            //正负对比图 
            PRChart pr = new PRChart();
            pr.xAxis = new List<dynamic>();
            pr.data1 = new List<double>(); //利润
            pr.data2 = new List<double>(); //收入
            pr.data3 = new List<double>(); //指出
            if (list[3] != null && list[3].Count > 0)
            {
                List<dynamic> il = list[3];
                var orgs = il.Select(o => o.OrgName).Distinct();
                pr.xAxis = orgs.ToList();
                foreach (string org in pr.xAxis)
                {


                    var data = from p in il
                               where p.OrgName.ToString() == org
                               select p;

                    if (data == null) continue;

                    double dt2 = 0, dt3 = 0;
                    var d2 = data.FirstOrDefault(o => o.FeeType.ToString() == "应收");
                    if (d2 == null) pr.data2.Add(0);
                    else
                    {
                        dt2 = d2.kWh;
                        pr.data2.Add(d2.kWh);
                    }
                    var d3 = data.FirstOrDefault(o => o.FeeType.ToString() == "应付");
                    if (d3 == null) pr.data3.Add(0);
                    else
                    {
                        dt3 = d3.kWh;
                        pr.data3.Add(d3.kWh * -1);
                    }
                    var dt1 = Math.Round(dt2 - dt3, 2);
                    pr.data1.Add(dt1);

                }

            }
            ret.Add(pr);

            if (list[4] != null && list[4].Count > 0)
            {
                ret.Add(list[4]);
            }

            return ret.ToJson();
        }
        //单相表 历史 曲线
        public string Get_History_Meter_1(string typeTime, string startTime, string endTime, string deviceId)
        {
            string proc = string.Format("Get_History_Meter_1 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, deviceId);

            var list = db.QueryList<dynamic>(proc, null); //功率 电流 电压 频率 功率因数 千乏 千伏安

            //当日各站点 电量分布 kW 
            LineChart lc = new LineChart();
            lc.xAxis = new List<dynamic>();
            lc.legend = new List<dynamic>() { "正向电能(kWh)", "反向电能(kWh)", "功率(W)", "电流(A)", "电压(V)", "乏(Var)", "伏安(VA)", "功率因素", "频率(Hz)" };
            lc.series = new List<LineSerie>();
            LineSerie kWh_P = new LineSerie()
            {
                type = "line",
                name = "正向电能(kWh)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie kWh_R = new LineSerie()
            {
                type = "line",
                name = "反向电能(kWh)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie W = new LineSerie()
            {
                type = "line",
                name = "功率(W)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie A = new LineSerie()
            {
                type = "line",
                name = "电流(A)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie V = new LineSerie()
            {
                type = "line",
                name = "电压(V)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie Var = new LineSerie()
            {
                type = "line",
                name = "乏(Var)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie VA = new LineSerie()
            {
                type = "line",
                name = "伏安(VA)",
                smooth = true,
                data = new List<double>()
            };
            LineSerie PT = new LineSerie()
            {
                type = "line",
                name = "功率因素",
                smooth = true,
                data = new List<double>()
            };
            LineSerie Hz = new LineSerie()
            {
                type = "line",
                name = "频率(Hz)",
                smooth = true,
                data = new List<double>()
            };
            if (list != null && list.Count() > 0)
            {
                List<dynamic> il = list.ToList();
                lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT));
                foreach (var row in il)
                {
                    kWh_P.data.Add(row.kWh_P);
                    kWh_R.data.Add(row.kWh_R);
                    W.data.Add(row.W);
                    A.data.Add(row.A);
                    V.data.Add(row.V);
                    Var.data.Add(row.Var);
                    VA.data.Add(row.VA);
                    PT.data.Add(row.PT);
                    Hz.data.Add(row.Hz);
                }
            }
            else
            {
                kWh_P.data.Add(0);
                kWh_R.data.Add(0);
                W.data.Add(0);
                A.data.Add(0);
                V.data.Add(0);
                Var.data.Add(0);
                VA.data.Add(0);
                PT.data.Add(0);
                Hz.data.Add(0);
            }

            lc.series.Add(kWh_P);
            lc.series.Add(kWh_R);
            lc.series.Add(W);
            lc.series.Add(A);
            lc.series.Add(V);
            lc.series.Add(Var);
            lc.series.Add(VA);
            lc.series.Add(PT);
            lc.series.Add(Hz);

            return lc.ToJson();

        }
        //三相表 历史 曲线
        public string Get_History_Meter_3(string typeTime, string startTime, string endTime, string deviceId, string lineType)
        {
            string proc = string.Format("Get_History_Meter_3 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, deviceId);

            var list = db.QueryList<dynamic>(proc, null); //功率 电流 电压 频率 功率因数 千乏 千伏安

            //当日各站点 电量分布 kW 
            LineChart lc = new LineChart();
            lc.xAxis = new List<dynamic>();
            if (lineType == "kWh")
                lc.legend = new List<dynamic>()
            {
             "正向电能(kWh)",
             "反向电能(kWh)",
            };

            if (lineType == "V")
                lc.legend = new List<dynamic>()
            {
             "A相_电压(V)",
             "B相_电压(V)",
             "C相_电压(V)",

             "AB相_电压(V)",
             "BC相_电压(V)",
             "CA相_电压(V)",
                };

            if (lineType == "A")
                lc.legend = new List<dynamic>()
            {
             "A相_电流(A)",
             "B相_电流(A)",
             "C相_电流(A)",
                };
            if (lineType == "kW")
                lc.legend = new List<dynamic>()
            {
             "A相_功率(kW)",
             "B相_功率(kW)",
             "C相_功率(kW)",
             "总功率(kW)",
                };
            if (lineType == "kVar")
                lc.legend = new List<dynamic>()
            {
             "A相_千乏(kVar)",
             "B相_千乏(kVar)",
             "C相_千乏(kVar)",
             "总千乏(kVar)",
                };
            if (lineType == "kVA")
                lc.legend = new List<dynamic>()
            {
             "A相_千伏安(kVA)",
             "B相_千伏安(kVA)",
             "C相_千伏安(kVA)",
             "总千伏安(kVA)",
                };
            if (lineType == "PT")
                lc.legend = new List<dynamic>()
            {
             "A相_功率因素",
             "B相_功率因素",
             "C相_功率因素",
             "总功率因素",
                };
            if (lineType == "Hz")
                lc.legend = new List<dynamic>()
            {
             "频率(Hz)" };
            lc.series = new List<LineSerie>();
            foreach (string legendName in lc.legend)
            {
                LineSerie serie = new LineSerie()
                {
                    type = "line",
                    name = legendName,
                    smooth = true,
                    data = new List<double>()
                };
                lc.series.Add(serie);
            }
            if (list != null && list.Count() > 0)
            {
                List<dynamic> il = list.ToList();
                lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT));
                foreach (var row in il)
                {
                    if (lineType == "kWh")
                    {
                        lc.series[0].data.Add(row.kWh_P);
                        lc.series[1].data.Add(row.kWh_R);
                    }
                    if (lineType == "V")
                    {
                        lc.series[0].data.Add(row.V_A);
                        lc.series[1].data.Add(row.V_B);
                        lc.series[2].data.Add(row.V_C);
                        lc.series[3].data.Add(row.V_AB);
                        lc.series[4].data.Add(row.V_BC);
                        lc.series[5].data.Add(row.V_CA);
                    }
                    if (lineType == "A")
                    {
                        lc.series[0].data.Add(row.A_A);
                        lc.series[1].data.Add(row.A_B);
                        lc.series[2].data.Add(row.A_C);
                    }
                    if (lineType == "kW")
                    {
                        lc.series[0].data.Add(row.kW_A);
                        lc.series[1].data.Add(row.kW_B);
                        lc.series[2].data.Add(row.kW_C);
                        lc.series[3].data.Add(row.kW_Total);
                    }
                    if (lineType == "kVar")
                    {
                        lc.series[0].data.Add(row.kVar_A);
                        lc.series[1].data.Add(row.kVar_B);
                        lc.series[2].data.Add(row.kVar_C);
                        lc.series[3].data.Add(row.kVar_Total);
                    }
                    if (lineType == "kVA")
                    {
                        lc.series[0].data.Add(row.kVA_A);
                        lc.series[1].data.Add(row.kVA_B);
                        lc.series[2].data.Add(row.kVA_C);
                        lc.series[3].data.Add(row.kVA_Total);
                    }
                    if (lineType == "PT")
                    {
                        lc.series[0].data.Add(row.PF_A);
                        lc.series[1].data.Add(row.PF_B);
                        lc.series[2].data.Add(row.PF_C);
                        lc.series[3].data.Add(row.PF_Total);
                    }
                    if (lineType == "Hz")
                    {
                        lc.series[0].data.Add(row.Hz);
                    }
                }
            }
            else
            {
                if (lineType == "kWh")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                }
                if (lineType == "V")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                    lc.series[3].data.Add(0);
                    lc.series[4].data.Add(0);
                    lc.series[5].data.Add(0);
                }
                if (lineType == "A")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                }
                if (lineType == "kW")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                    lc.series[3].data.Add(0);
                }
                if (lineType == "kVar")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                    lc.series[3].data.Add(0);
                }
                if (lineType == "kVA")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                    lc.series[3].data.Add(0);
                }
                if (lineType == "PT")
                {
                    lc.series[0].data.Add(0);
                    lc.series[1].data.Add(0);
                    lc.series[2].data.Add(0);
                    lc.series[3].data.Add(0);
                }
                if (lineType == "Hz")
                {
                    lc.series[0].data.Add(0);
                }
            }
            return lc.ToJson();

        }
        //温湿度 历史 曲线
        public string Get_History_Meter_2(string typeTime, string startTime, string endTime, string deviceId)
        {
            string proc = string.Format("Get_History_Meter_2 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, deviceId);

            var list = db.QueryList<dynamic>(proc, null); //功率 电流 电压 频率 功率因数 千乏 千伏安

            //当日各站点 电量分布 kW 
            LineChart lc = new LineChart();
            lc.xAxis = new List<dynamic>();
            lc.legend = new List<dynamic>()
            {
             "温度(℃)",
             "相对湿度(%RH)",
            };
            lc.series = new List<LineSerie>();
            foreach (string legendName in lc.legend)
            {
                LineSerie serie = new LineSerie()
                {
                    type = "line",
                    name = legendName,
                    smooth = true,
                    data = new List<double>()
                };
                lc.series.Add(serie);
            }
            if (list != null && list.Count() > 0)
            {
                List<dynamic> il = list.ToList();
                lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT));
                foreach (var row in il)
                {
                    lc.series[0].data.Add(row.C);
                    lc.series[1].data.Add(row.RH);
                }
            }
            else
            {

                lc.series[0].data.Add(0);
                lc.series[1].data.Add(0);
            }
            return lc.ToJson();

        }


        //单相表 对比
        public string Get_Comparisons_Meter_1(string typeTime, string startTime, string endTime, string devs, string lineType)
        {
            if (string.IsNullOrEmpty(devs)) return "{}";

            List<string> devList = devs.ToObject<List<string>>();
            string devIds = string.Join(",", devList.ToArray());
            string proc = string.Format("Get_Comparisons_Meter_1 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, devIds);

            var list = db.QueryMultiple(proc, null, 2, System.Data.CommandType.Text); //功率 电流 电压 频率 功率因数 千乏 千伏安

            if (list != null && list.Count == 2)
            {
                //当日各站点 电量分布 kW 
                LineChart lc = new LineChart();
                lc.xAxis = new List<dynamic>();
                //string xjson = list[0].ToJson();
                lc.legend = new List<dynamic>();
                foreach (dynamic leg in list[0])
                {
                    lc.legend.Add(leg.DeviceName);
                }
                //xjson.ToObject<List<dynamic>>();
                lc.series = new List<LineSerie>();
                foreach (string legendName in lc.legend)
                {
                    LineSerie serie = new LineSerie()
                    {
                        type = "bar",
                        name = legendName,
                        smooth = true,
                        data = new List<double>()
                    };
                    lc.series.Add(serie);
                }
                if (list[1] != null && list[1].Count > 0)
                {
                    List<dynamic> il = list[1];
                    lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT).Distinct());
                    foreach (var row in il)
                    {
                        for (int i = 0; i < lc.series.Count; i++)
                        {
                            if (row.DeviceName != lc.series[i].name) continue;//跳过
                            if (lineType == "kWh1")
                                lc.series[i].data.Add(row.kWh_P);
                            else if (lineType == "kWh2")
                                lc.series[i].data.Add(row.kWh_R);
                            else if (lineType == "W")
                                lc.series[i].data.Add(row.W);
                            else if (lineType == "A")
                                lc.series[i].data.Add(row.A);
                            else if (lineType == "V")
                                lc.series[i].data.Add(row.V);
                            else if (lineType == "Var")
                                lc.series[i].data.Add(row.Var);
                            else if (lineType == "VA")
                                lc.series[i].data.Add(row.VA);
                            else if (lineType == "PT")
                                lc.series[i].data.Add(row.PT);
                            else if (lineType == "Hz")
                                lc.series[i].data.Add(row.Hz);
                        }

                    }
                }
                else
                {
                    for (int i = 0; i < lc.series.Count; i++)
                    {
                        lc.series[i].data.Add(0);
                    }
                }
                return lc.ToJson();
            }
            else
            {
                return "{}";
            }
        }

        //单相表 对比
        public string Get_Comparisons_Meter_2(string typeTime, string startTime, string endTime, string devs, string lineType)
        {
            if (string.IsNullOrEmpty(devs)) return "{}";

            List<string> devList = devs.ToObject<List<string>>();
            string devIds = string.Join(",", devList.ToArray());
            string proc = string.Format("Get_Comparisons_Meter_2 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, devIds);

            var list = db.QueryMultiple(proc, null, 2, System.Data.CommandType.Text); //功率 电流 电压 频率 功率因数 千乏 千伏安

            if (list != null && list.Count == 2)
            {
                //当日各站点 电量分布 kW 
                LineChart lc = new LineChart();
                lc.xAxis = new List<dynamic>();
                //string xjson = list[0].ToJson();
                lc.legend = new List<dynamic>();
                foreach (dynamic leg in list[0])
                {
                    lc.legend.Add(leg.DeviceName);
                }
                //xjson.ToObject<List<dynamic>>();
                lc.series = new List<LineSerie>();
                foreach (string legendName in lc.legend)
                {
                    LineSerie serie = new LineSerie()
                    {
                        type = "bar",
                        name = legendName,
                        smooth = true,
                        data = new List<double>()
                    };
                    lc.series.Add(serie);
                }
                if (list[1] != null && list[1].Count > 0)
                {
                    List<dynamic> il = list[1];
                    lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT).Distinct());
                    foreach (var row in il)
                    {
                        for (int i = 0; i < lc.series.Count; i++)
                        {
                            if (row.DeviceName != lc.series[i].name) continue;//跳过
                            if (lineType == "C")
                                lc.series[i].data.Add(row.C);
                            else if (lineType == "RH")
                                lc.series[i].data.Add(row.RH);

                        }

                    }
                }
                else
                {
                    for (int i = 0; i < lc.series.Count; i++)
                    {
                        lc.series[i].data.Add(0);
                    }
                }
                return lc.ToJson();
            }
            else
            {
                return "{}";
            }
        } 

        //三相表 对比
        public string Get_Comparisons_Meter_3(string typeTime, string startTime, string endTime, string devs, string lineType)
        {
            if (string.IsNullOrEmpty(devs)) return "{}";

            List<string> devList = devs.ToObject<List<string>>();
            string devIds = string.Join(",", devList.ToArray());
            string proc = string.Format("Get_Comparisons_Meter_3 '{0}','{1}','{2}','{3}'", typeTime, startTime, endTime, devIds);

            var list = db.QueryMultiple(proc, null, 2, System.Data.CommandType.Text); //功率 电流 电压 频率 功率因数 千乏 千伏安

            if (list != null && list.Count == 2)
            {
                //当日各站点 电量分布 kW 
                LineChart lc = new LineChart();
                lc.xAxis = new List<dynamic>();
                //string xjson = list[0].ToJson();
                lc.legend = new List<dynamic>();
                foreach (dynamic leg in list[0])
                {
                    lc.legend.Add(leg.DeviceName);
                }
                //xjson.ToObject<List<dynamic>>();
                lc.series = new List<LineSerie>();
                foreach (string legendName in lc.legend)
                {
                    LineSerie serie = new LineSerie()
                    {
                        type = "bar",
                        name = legendName,
                        smooth = true,
                        data = new List<double>()
                    };
                    lc.series.Add(serie);
                }
                if (list[1] != null && list[1].Count > 0)
                {
                    List<dynamic> il = list[1];
                    lc.xAxis.AddRange(il.OrderBy(p => p.DT).Select(o => o.DT));
                    foreach (var row in il)
                    {
                        for (int i = 0; i < lc.series.Count; i++)
                        {
                            if (row.DeviceName != lc.series[i].name) continue;//跳过 

                            if (lineType == "kWh1")
                                lc.series[i].data.Add(row.kWh_P);
                            else if (lineType == "kWh2")
                                lc.series[i].data.Add(row.kWh_R); 
                            else if (lineType == "V_A")
                                lc.series[i].data.Add(row.V_A);
                            else if (lineType == "V_B")
                                lc.series[i].data.Add(row.V_B);
                            else if (lineType == "V_C")
                                lc.series[i].data.Add(row.V_C);

                            else if (lineType == "V_AB")
                                lc.series[i].data.Add(row.V_AB);
                            else if (lineType == "V_BC")
                                lc.series[i].data.Add(row.V_BC);
                            else if (lineType == "V_CA")
                                lc.series[i].data.Add(row.V_CA);

                            else if (lineType == "A_A")
                                lc.series[i].data.Add(row.A_A);
                            else if (lineType == "A_B")
                                lc.series[i].data.Add(row.A_B);
                            else if (lineType == "A_C")
                                lc.series[i].data.Add(row.A_C);

                            else if (lineType == "kW_A")
                                lc.series[i].data.Add(row.kW_A);
                            else if (lineType == "kW_B")
                                lc.series[i].data.Add(row.kW_B);
                            else if (lineType == "kW_C")
                                lc.series[i].data.Add(row.kW_C);
                            else if (lineType == "kW")
                                lc.series[i].data.Add(row.kW_Total);

                            else if (lineType == "kVar_A")
                                lc.series[i].data.Add(row.kVar_A);
                            else if (lineType == "kVar_B")
                                lc.series[i].data.Add(row.kVar_B);
                            else if (lineType == "kVar_C")
                                lc.series[i].data.Add(row.kVar_C);
                            else if (lineType == "kVar")
                                lc.series[i].data.Add(row.kVar_Total);


                            else if (lineType == "kVA_A")
                                lc.series[i].data.Add(row.kVA_A);
                            else if (lineType == "kVA_B")
                                lc.series[i].data.Add(row.kVA_B);
                            else if (lineType == "kVA_C")
                                lc.series[i].data.Add(row.kVA_C);
                            else if (lineType == "kVA")
                                lc.series[i].data.Add(row.kVA_Total);

                            else if (lineType == "PF_A")
                                lc.series[i].data.Add(row.PF_A);
                            else if (lineType == "PF_B")
                                lc.series[i].data.Add(row.PF_B);
                            else if (lineType == "PF_C")
                                lc.series[i].data.Add(row.PF_C);
                            else if (lineType == "PF")
                                lc.series[i].data.Add(row.PF_Total);  
                           
                            else if (lineType == "Hz")
                                lc.series[i].data.Add(row.Hz);
                        }

                    }
                }
                else
                {
                    for (int i = 0; i < lc.series.Count; i++)
                    {
                        lc.series[i].data.Add(0);
                    }
                }
                return lc.ToJson();
            }
            else
            {
                return "{}";
            }
        }






    }

}
